var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testdb');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
    // we are connected!
});

var kittySchema = mongoose.Schema({
    name: String
});

kittySchema.methods.speak = function() {
    var greeting = this.name ? "Meow name is " + this.name : "i dont have a name";
    console.log(greeting);
}

var Kitten = mongoose.model('Kitten', kittySchema);

var silence = new Kitten({name: 'Silence'});
console.log(silence.name);

var fluffy = new Kitten({name: 'fluffy'});
fluffy.speak();

//保存到数据库里
silence.save();

fluffy.save(function (err, fluffy) {
    if (err) return console.error(err);
    fluffy.speak();
});


// Kitten.find(function (err, kittens) {
//     if (err) return console.error(err);
//     console.log(kittens);
// });
//
// Kitten.find({name: /^fluff/}, callback);